// pages/wo/candidatesInvited/candidatesInvited.js
import request from '../../../utils/http.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isTab:false,



    winHeight:0,
    dianPuId: 0,
    screenHeight: 0,



    count: 0, //总人数
    xiaJiList: [],



    listYongJin:[],


   
  

    hiddenPopPaper:false,
    itemFrom_url:'',
    itemFrom_name:'',
    itemFrom_phone:'',
    itemFrom_cardNum:'',

    itemTo_url:'',
    itemTo_name:'',
    itemTo_phone:'',
    itemTo_cardNum:'',
    select_index:0,
    toSelect_index:0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      screenHeight: wx.getSystemInfoSync().windowHeight,
      dianPuId: wx.getStorageSync(app.globalData.SHOPID) == '' ? 0 : wx.getStorageSync(app.globalData.SHOPID)
    })
 
    this.getJKHYList();

    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight:  res.windowHeight-(100/750 * res.windowWidth),
        });
      }
    });
  },


  //切换
  changeTap() {
    this.setData({
      isTab:!this.data.isTab
    })
  },

  getDaShiInfo:function(){
    var that = this

    wx.showLoading({
      title: '加载中...',
      mask: true
    })

    let data = {
      dianPuId: that.data.dianPuId,
    }

    request.get('jianKangDaShi/renWu', data, res => {
    console.log("大使基本信息:" + JSON.stringify(res.data))
        that.setData({
          isDianzhu:res.data.isDianZhu==1?true:false,
          isDashi:res.data.isDaShi==1?true:false,
          daShiShenFenZheng:res.data.daShiShenFenZheng,
          listRenWu:res.data.listRenWu,
          listYongJin:res.data.listYongJin,
        })

        if(res.data.isDianZhu==1){
          that.getDaShiList();
        }


      wx.hideLoading()
    }, err => {
      wx.hideLoading()
      wx.showToast({
        title: '查询失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
  
    })
  },

  //获取店铺会员
  getJKHYList: function () {

    var that = this

      wx.showLoading({
        title: '加载中...',
        mask: true
      })

      let data = {
        dianPuId: that.data.dianPuId,
      }

      request.get('person/getJianKangHuiXiaJiList', data, res => {
      console.log("用户List:" + JSON.stringify(res.data))
          that.setData({
            count: res.data.count, //总人数
            xiaJiList: res.data.xiaJiList,
          })

        wx.hideLoading()
      }, err => {
        wx.hideLoading()
        wx.showToast({
          title: '查询失败', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 1000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
    
      })
  
  },




  tabItem:function(e){
    if(this.data.isDianzhu){

      let index = e.currentTarget.dataset.index //类型

      if(this.data.daShiList[index].tuiJianRenShenFenZheng!=''){

        wx.showToast({
          title: '推荐关系已经绑定', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 2000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })

      }else{
        this.setData({
          itemTo_url:this.data.daShiList[index].daShiTouXiangUrl==''?'/image/touxiang.png':this.data.daShiList[index].daShiTouXiangUrl,
          itemTo_name:this.data.daShiList[index].daShiMingCheng==''?'微信用户':this.data.daShiList[index].daShiMingCheng,
          itemTo_phone:this.data.daShiList[index].daShiPhone,
          itemTo_cardNum:this.data.daShiList[index].daShiShenFenZheng,
        
        })
        
        this.getTuiJianDaShiList(this.data.daShiList[index].daShiShenFenZheng);
      }
    }
  },


  getTuiJianDaShiList:function(daShiShenFenZheng){
    var that = this

      wx.showLoading({
        title: '加载中...',
        mask: true
      })

      let data = {
        dianPuId: that.data.dianPuId,
        daShiShenFenZheng:daShiShenFenZheng,
      }

      request.get('jianKangDaShi/tuiJianRenList', data, res => {
      // console.log("推荐大使List:" + JSON.stringify(res.data))


      if(res.data.length>0){
        that.setData({
          hiddenPopPaper:true,
          tuiJianDSList:res.data,
          itemFrom_cardNum:res.data[0].daShiShenFenZheng,
        
        })
      }else{
        wx.showToast({
          title: '没有更早的大使可以绑定', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 2000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
      }



        wx.hideLoading()
      }, err => {
        wx.hideLoading()
        wx.showToast({
          title: '查询失败', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 1000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
    
      })

  },



  bindPickerChange(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    var index=e.detail.value
    this.setData({
      toSelect_index: index,
      itemFrom_url:this.data.tuiJianDSList[index].daShiTouXiangUrl==''?'/image/touxiang.png':this.data.tuiJianDSList[index].daShiTouXiangUrl,
      itemFrom_name:this.data.tuiJianDSList[index].daShiMingCheng==''?'微信用户':this.data.tuiJianDSList[index].daShiMingCheng,
      itemFrom_phone:this.data.tuiJianDSList[index].daShiPhone,
      itemFrom_cardNum:this.data.tuiJianDSList[index].daShiShenFenZheng,
    })
  },

  //发起绑定
  bindBangDing(){

    var that=this
    wx.showModal({
      title: '温馨提示',
      content: '推荐关系一经绑定，将无法修改，确认绑定吗？',
      showCancel: true,
      confirmText:'确定',
      cancelText:'返回',
      success(res) {
          if (res.confirm) {
            that.daShiLinkAction();
          }
      }
  })
  },
  

  //绑定推荐关系
  daShiLinkAction:function(){
    var that = this


    wx.showLoading({
      title: '处理中...',
      mask: true
    })

    let data = {
      dianPuId: that.data.dianPuId,
      daShiShenFenZheng:that.data.itemTo_cardNum,
      tuiJianRenShenFenZheng:that.data.itemFrom_cardNum,
    }

    request.get('jianKangDaShi/bangDingTuiJianRen', data, res => {
    // console.log("绑定:" + JSON.stringify(res.data))

    if(res.data.status==1){


      that.getDaShiInfo();
      that.closePop();
      wx.showToast({
        title: '绑定成功！', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })


    }else{
      wx.showToast({
        title: '绑定失败！', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    }
 

      wx.hideLoading()
    }, err => {
      wx.hideLoading()
      wx.showToast({
        title: '查询失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
  
    })
  },



  closePop(){
    this.setData({
      hiddenPopPaper:false,
      toSelect_index:0,
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },



 
})